package com.jindi.jacococli.biz.config;

import com.jindi.jacococli.biz.constant.Constant;
import com.jindi.jacococli.biz.utils.JSONUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.jindi.jacococli.biz.constant.Constant.MASTER_BRANCH;

@Getter
@Setter
@Component
public class AppProperties {

    @Value("${app.workSpace}")
    private String workSpace = "/opt/workspace/jacoco/";
    @Value("${app.gitDir}")
    private String gitDir = "/opt/workspace/git/";
    @Value("${app.jacocoServerPort}")
    private int jacocoServerPort = Constant.JACOCO_PORT;
    @Value("${app.gitLabDomain}")
    private String gitLabDomain;
    @Value("${app.accessToken}")
    private String accessToken;
    @Value("${app.excludePackages}")
    private Set<String> excludePackages;
    @Value("${app.projectBranch}")
    private String projectBranch;

    private Map<String, String> branchMap;

    public String getBranch(String projectName) {
        if (Objects.isNull(branchMap)) {
            branchMap = JSONUtils.parseObject(projectBranch, Map.class);
        }
        String specialBranch = branchMap.get(projectName);
        if (StringUtils.isNoneBlank(specialBranch)) {
            return specialBranch;
        }
        return MASTER_BRANCH;
    }

}
